using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;

/// <summary>
/// The TeacherSalaryTableDB class is responsible for interacting with the database to retrieve and store information
/// about TeacherSalaryTable objects.
/// </summary>
public class TeacherSalaryTableDB

{
#region "Public Methods"

/// <summary>
/// Gets an instance of TeacherSalaryTable from the underlying datasource.
/// </summary>
/// <param name="teacherSalaryKey">The unique TeacherSalaryKey of the TeacherSalaryTable in the database.</param>
/// <returns>An TeacherSalaryTable when the TeacherSalaryKey was found in the database, or null otherwise.</returns>
public static TeacherSalaryTable GetItem(int teacherSalaryKey)
{
TeacherSalaryTable myTeacherSalaryTable = null;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocTeacherSalaryTableSelectSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@teacherSalaryKey", teacherSalaryKey);

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.Read()) {
myTeacherSalaryTable = FillDataRecord(myReader);
}
myReader.Close();
}
myConnection.Close();
}
return myTeacherSalaryTable;
}
}

/// <summary>
/// Returns a list with TeacherSalaryTable objects.
/// </summary>
/// <returns>A generics List with the TeacherSalaryTable objects.</returns>
public static TeacherSalaryTableList GetList()
{
TeacherSalaryTableList tempList = new TeacherSalaryTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocTeacherSalaryTableSelectList", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
}
return tempList;
}

public static TeacherSalaryTableList GetList(int UserAccountKey)
{
    TeacherSalaryTableList tempList = new TeacherSalaryTableList();
    using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
    {
        using (SqlCommand myCommand = new SqlCommand("sprocTeacherSalaryTableSelectList", myConnection))
        {
            myCommand.CommandType = CommandType.StoredProcedure;
            myCommand.Parameters.AddWithValue("@userAccountKey", UserAccountKey);

            myConnection.Open();
            using (SqlDataReader myReader = myCommand.ExecuteReader())
            {
                if (myReader.HasRows)
                {
                    while (myReader.Read())
                    {
                        tempList.Add(FillDataRecord(myReader));
                    }
                }
                myReader.Close();
            }
        }
    }
    return tempList;
}

/// <summary>
/// Returns a list with TeacherSalaryTable objects.
/// </summary>
/// <returns>A generics List with the TeacherSalaryTable objects.</returns>
public static TeacherSalaryTableList GetListByCurrencyCode(Int16 CurrencyCode)
{
TeacherSalaryTableList tempList = new TeacherSalaryTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocTeacherSalaryTableSelectListByCurrencyCode", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@currencyCode", CurrencyCode);
myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
return tempList;
}
}

/// <summary>
/// Returns a list with TeacherSalaryTable objects.
/// </summary>
/// <returns>A generics List with the TeacherSalaryTable objects.</returns>
public static TeacherSalaryTableList GetListByPaymentTypeCode(Int16 PaymentTypeCode)
{
TeacherSalaryTableList tempList = new TeacherSalaryTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocTeacherSalaryTableSelectListByPaymentTypeCode", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@paymentTypeCode", PaymentTypeCode);
myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
return tempList;
}
}

/// <summary>
/// Returns a list with TeacherSalaryTable objects.
/// </summary>
/// <returns>A generics List with the TeacherSalaryTable objects.</returns>
public static TeacherSalaryTableList GetListByUserAccountKey(int UserAccountKey)
{
TeacherSalaryTableList tempList = new TeacherSalaryTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocTeacherSalaryTableSelectListByUserAccountKey", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@userAccountKey", UserAccountKey);
myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
return tempList;
}
}

/// <summary>
/// Saves a TeacherSalaryTable in the database.
/// </summary>
/// <param name="myTeacherSalaryTable">The TeacherSalaryTable instance to save.</param>
/// <returns>The new TeacherSalaryKey if the TeacherSalaryTable is new in the database or the existing TeacherSalaryKey when an item was updated.</returns>
public static int Save(TeacherSalaryTable myTeacherSalaryTable)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocTeacherSalaryTableInsertUpdateSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

if (myTeacherSalaryTable.TeacherSalaryKey == -1)
{myCommand.Parameters.AddWithValue("@teacherSalaryKey", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@teacherSalaryKey", myTeacherSalaryTable.TeacherSalaryKey);
}
if (myTeacherSalaryTable.UserAccountKey == null){
myCommand.Parameters.AddWithValue("@userAccountKey", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@userAccountKey", myTeacherSalaryTable.UserAccountKey);
}
if (myTeacherSalaryTable.PaymentTypeCode == null){
myCommand.Parameters.AddWithValue("@paymentTypeCode", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@paymentTypeCode", myTeacherSalaryTable.PaymentTypeCode);
}
if (myTeacherSalaryTable.Amount == null){
myCommand.Parameters.AddWithValue("@amount", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@amount", myTeacherSalaryTable.Amount);
}
if (myTeacherSalaryTable.Allotment1 == null){
myCommand.Parameters.AddWithValue("@allotment1", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@allotment1", myTeacherSalaryTable.Allotment1);
}
if (myTeacherSalaryTable.Allotment2 == null){
myCommand.Parameters.AddWithValue("@allotment2", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@allotment2", myTeacherSalaryTable.Allotment2);
}
if (myTeacherSalaryTable.CurrencyCode == null){
myCommand.Parameters.AddWithValue("@currencyCode", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@currencyCode", myTeacherSalaryTable.CurrencyCode);
}

if (myTeacherSalaryTable.StartDate == null)
{
    myCommand.Parameters.AddWithValue("@StartDate", DBNull.Value);
}
else
{
    myCommand.Parameters.AddWithValue("@StartDate", myTeacherSalaryTable.StartDate);
}
if (myTeacherSalaryTable.EndDate == null)
{
    myCommand.Parameters.AddWithValue("@EndDate", DBNull.Value);
}
else
{
    myCommand.Parameters.AddWithValue("@EndDate", myTeacherSalaryTable.EndDate);
}

if (myTeacherSalaryTable.Remarks == null){
myCommand.Parameters.AddWithValue("@remarks", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@remarks", myTeacherSalaryTable.Remarks);
}
if (myTeacherSalaryTable.Active == null){
myCommand.Parameters.AddWithValue("@active", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@active", myTeacherSalaryTable.Active);
}
if (myTeacherSalaryTable.Status == null){
myCommand.Parameters.AddWithValue("@status", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@status", myTeacherSalaryTable.Status);
}
if (myTeacherSalaryTable.CreatedDateTime == null){
myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@createdDateTime", myTeacherSalaryTable.CreatedDateTime);
}
if (myTeacherSalaryTable.ModifiedDateTime == null){
myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@modifiedDateTime", myTeacherSalaryTable.ModifiedDateTime);
}

DbParameter returnValue;
returnValue = myCommand.CreateParameter();
returnValue.Direction = ParameterDirection.ReturnValue;
myCommand.Parameters.Add(returnValue);

myConnection.Open();
myCommand.ExecuteNonQuery();
result = Convert.ToInt32(returnValue.Value);
myConnection.Close();
}
}
return result;
}

/// <summary>
/// Deletes a TeacherSalaryTable from the database.
/// </summary>
/// <param name="teacherSalaryKey">The TeacherSalaryKey of the TeacherSalaryTable to delete.</param>
/// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
public static bool Delete(int teacherSalaryKey)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocTeacherSalaryTableDeleteSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myCommand.Parameters.AddWithValue("@teacherSalaryKey", teacherSalaryKey);
myConnection.Open();
result = myCommand.ExecuteNonQuery();
myConnection.Close();
}
}
return result > 0;
}

#endregion

/// <summary>
/// Initializes a new instance of the TeacherSalaryTable class and fills it with the data fom the IDataRecord.
/// </summary>
private static TeacherSalaryTable FillDataRecord(IDataRecord myDataRecord )
{
TeacherSalaryTable myTeacherSalaryTable = new TeacherSalaryTable();
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("TeacherSalaryKey")))
{
myTeacherSalaryTable.TeacherSalaryKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("TeacherSalaryKey"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("UserAccountKey")))
{
myTeacherSalaryTable.UserAccountKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("UserAccountKey"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("PaymentTypeCode")))
{
myTeacherSalaryTable.PaymentTypeCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("PaymentTypeCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Amount")))
{
myTeacherSalaryTable.Amount = myDataRecord.GetDecimal(myDataRecord.GetOrdinal("Amount"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Allotment1")))
{
myTeacherSalaryTable.Allotment1 = myDataRecord.GetDecimal(myDataRecord.GetOrdinal("Allotment1"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Allotment2")))
{
myTeacherSalaryTable.Allotment2 = myDataRecord.GetDecimal(myDataRecord.GetOrdinal("Allotment2"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CurrencyCode")))
{
myTeacherSalaryTable.CurrencyCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("CurrencyCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("StartDate")))
{
    myTeacherSalaryTable.StartDate = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("StartDate"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("EndDate")))
{
    myTeacherSalaryTable.EndDate = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("EndDate"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Remarks")))
{
myTeacherSalaryTable.Remarks = myDataRecord.GetString(myDataRecord.GetOrdinal("Remarks"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Active")))
{
myTeacherSalaryTable.Active = myDataRecord.GetBoolean(myDataRecord.GetOrdinal("Active"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
{
myTeacherSalaryTable.Status = myDataRecord.GetInt16(myDataRecord.GetOrdinal("Status"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
{
myTeacherSalaryTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
{
myTeacherSalaryTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
}
return myTeacherSalaryTable;
}
}
